package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

import static com.baomidou.mybatisplus.extension.toolkit.Db.count;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @Operation(summary = "查询品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(description = "品牌名称")  String name){
        //分页参数封装到了BaseController类
        startPage();
        //List<Brand> list = brandService.selectBrandList(name);
        LambdaQueryWrapper<Brand> brandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        brandLambdaQueryWrapper.like(StringUtils.hasText(name),Brand::getName,name);
        List<Brand> list = brandService.list(brandLambdaQueryWrapper);
        return getDataTable(list);
    }

    @Operation(summary = "获取品牌详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        //查所有
        //Brand brandServiceById = brandService.getById(id);
        //想查指定列
        LambdaQueryWrapper<Brand> brandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        brandLambdaQueryWrapper.eq(Brand::getId,id);
        brandLambdaQueryWrapper.select(Brand::getId,Brand::getName,Brand::getLogo);
        Brand brandServiceget = brandService.getOne(brandLambdaQueryWrapper);
        return success(brandServiceget);
    }

    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(@Parameter(description = "品牌") @RequestBody Brand brand) {
        brandService.add(brand);
        return AjaxResult.success();
        //设置当前用户
        /*String username = SecurityUtils.getUsername();
        brand.setCreateBy(username);
        //查询数据库中品牌是否重复
        long count = count(new LambdaQueryWrapper<Brand>().eq(Brand::getName,brand.getName()));
        if (count>0){
            throw new ServiceException("品牌重复");
        }
        brandService.save(brand);
        return AjaxResult.success();*/
    }
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(@Parameter(description = "品牌") @RequestBody Brand brand) {
        //设置当前用户
        brand.setUpdateBy(SecurityUtils.getUsername());
        brandService.updateById(brand);
        return AjaxResult.success();
    }
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "品牌id列表", example = "[1,2]")
            @PathVariable List<Long> ids) {
       brandService.removeBatchByIds(ids);
       return AjaxResult.success();
    }
    @Operation(summary = "查所有品牌")
    @GetMapping("getBrandAll")
    public AjaxResult getall(){
        List<Brand> list = brandService.list();
        return AjaxResult.success(list);
    }

}
